package com.example.ordersystem.controller;

import com.example.ordersystem.domain.Customer;
import com.example.ordersystem.domain.Merchant;
import com.example.ordersystem.service.CustomerService;
import com.example.ordersystem.service.MerchantService;
import com.example.ordersystem.tool.SendSMS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Controller
public class LoginRegisterController {
    private static final String CUSTOMER = "顾客";
    private static final String MERCHANT = "商家";

    @Autowired
    CustomerService customerService;
    @Autowired
    MerchantService merchantService;

    @RequestMapping("/registerPage")
    public String registerPage() {
        return "/register";
    }

    @RequestMapping("/SMSLogin")
    public String forgetPage() {
        return "SMSLogin";
    }

    @RequestMapping("/sendSMS")
    public void sendSMS(HttpServletRequest request, HttpServletResponse response, String tel) throws IOException {
        System.out.println(tel);
        System.out.println(customerService.alreadyRegistered(tel) != 0);
        if (customerService.alreadyRegistered(tel) != 0) {
            System.out.println("alreadyRegistered");

            String code = SendSMS.sendSMS(tel);
            System.out.println(code);
            request.getSession().setAttribute("code", code);
        } else {
            System.out.println("unregistered");
            request.setAttribute("unregistered", "该手机号尚未注册");
            response.sendRedirect("/index");
        }
    }

    /**
     * 账户-密码登录检查
     *
     * @param request  请求
     * @param account  账户
     * @param pwd      密码
     * @param identity 身份
     * @return 登录成功返回主页；登录失败重新登录
     * @throws UnsupportedEncodingException
     */
    @RequestMapping("/ap_loginCheck")
    public String login(HttpServletRequest request, String account, String pwd, String identity) throws UnsupportedEncodingException {

        try {
            /*转化为十进制数字*/
            int num = Integer.parseInt(account);
            if (CUSTOMER.equals(identity)) {
                Customer c = customerService.customerLoginCheck(num, pwd);
                if (c != null) {
                    request.getSession().setAttribute("customer", c);
                    /*普通用户登录成功*/
                    request.setAttribute("roomaddress",merchantService.findAllRoom());
                    return "forward:/customer/homePage";
                }
            } else if (MERCHANT.equals(identity)) {
                Merchant merchant = merchantService.merchantLoginCheck(num, pwd);
                if (merchant != null) {
                    request.getSession().setAttribute("merchant", merchant);
                    /*管理员登录成功*/
                    return "forward:/merchant/homePage";
                }
            }
        } catch (NumberFormatException e) {
        }

        request.setAttribute("error", "用户或密码错误");
        return "forward:/";
    }

    /**
     * 手机-验证码登录
     *
     * @return
     */
    @RequestMapping("/tel_loginCheck")
    public String telLogin(HttpServletRequest request, HttpServletResponse response, String tel, String verifyCode, String identity) {

        String code = (String) request.getSession().getAttribute("code");

        /*code不等于null,说明验证码发送成功*/
        if (code != null && code.equals(verifyCode)) {

            /*普通用户登录*/
            if (CUSTOMER.equals(identity)) {
                Customer c = customerService.customerSMSLoginCheck(tel);
                if (c != null) {
                    /*普通用户登录成功*/
                    request.getSession().setAttribute("customer", c);
                    request.setAttribute("roomaddress",merchantService.findAllRoom());
                    return "forward:/customer/homePage";
                }
            }
            /*商家登录*/
            else if (MERCHANT.equals(identity)) {
                Merchant merchant = merchantService.merchantSMSLoginCheck(tel);
                if (merchant != null) {
                    /*商家登录成功*/
                    request.getSession().setAttribute("merchant", merchant);
                    return "forward:/merchant/homePage";
                }
            }
        }
        request.setAttribute("verifyCodeError", "验证码错误");
        return "forward:/SMSLogin";
    }

    @RequestMapping("/register")
    public String register(HttpServletRequest request, String tel, String verifyCode, String username, String password, String rePwd) throws UnsupportedEncodingException {
        /*验证码*/
        String code = (String) request.getSession().getAttribute("code");
        System.out.println(rePwd);
        System.out.println(username);
        System.out.println(password);
        System.out.println(verifyCode);
        System.out.println(code);

            int account = customerService.alreadyRegistered(tel);
            /*手机号尚未注册*/
            if (account == 0) {
                Customer customer = new Customer();
                customer.setName(username);
                customer.setPwd(password);
                customer.setTel(tel);
                customerService.register(customer);
                /*新生成的账户*/
                request.setAttribute("account", customer.getNum());
            }
            /*手机号已经注册*/
            else {
                /*提示用户账户*/
                request.setAttribute("alreadyRegistered", account);
            }
            /*返回注册页面*/
        return "forward:/registerPage";
    }

}
